const API = require('../../http/api')
Page({
  //  返回上一页
  onClickLeft() {
    wx.navigateBack({
      delta: 1,
    })
  },
  // 轮播图数据渲染
  async getlist(id) {
    const res = await API.getgoodszqlist(id)
    console.log('商品详情', res);
    let {
      content
    } = this.data
    content = res.data.content.replace(/<img/g, '<img style="width:100%;height:auto;"')
    if (res.code == 0) {
      this.setData({
        lunbo: res.data,
        content
      })
    }
  },
  // 底部弹框显示
  isshow() {
    console.log('弹框显示');
    this.setData({
      show: true
    })
  },
  // 关闭弹框
  onClose() {
    this.setData({
      show: false,
      selected: [],
      goodsPrice: 0
    })
  },
  // 点击tab
  onchangetab(e) {
    console.log('change', e.detail.index);
    this.setData({
      active: e.detail.index
    })
    if (this.data.active == 0) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else if (this.data.active == 1) {
      wx.pageScrollTo({
        scrollTop: 780
      })
    } else if (this.data.active == 2) {
      wx.pageScrollTo({
        scrollTop: 29100
      })
    }
  },

  // onPageScroll(e) {
  //   if (e.scrollTop >= 800) {
  //     this.setData({
  //       active: 1
  //     })
  //   } else if (e.scrollTop == 0) {
  //     this.setData({
  //       active: 0
  //     })
  //   } else if (e.scrollTop >= 2910) {
  //     this.setData({
  //       active: 2
  //     })
  //   }
  // },

  // sku
  async sel(e) {
    console.log('sku', e.currentTarget.dataset.propertyid, e.currentTarget.dataset.id);
    let {
      selected,
      arrlist
    } = this.data
    const pid = e.currentTarget.dataset.propertyid
    const id = e.currentTarget.dataset.id
    //设置选中的颜色
    selected[pid] = id;
    arrlist[pid] = id;
    this.setData({
      selected,
      arrlist,
    })
    let propertyChildIds = "";
    for (var key in this.data.arrlist) {
      //id值的拼接效果
      propertyChildIds += key + ":" + this.data.arrlist[key] + ",";
    }
    console.log('arrlist', propertyChildIds);
    let obj = {
      goodsId: this.data.lunbo.basicInfo.id,
      token: wx.getStorageSync('token'),
      propertyChildIds: propertyChildIds
    }
    const res = await API.getgoodslistjg(obj)
    console.log('价格', res);
    if (res.code == 0) {
      console.log(res.data.price);
      this.setData({
        goodsPrice: res.data.price
      })
    }
  },
  // 加入购物车
  async add() {
    if (this.data.lunbo.basicInfo.propertyIds || this.data.lunbo.basicInfo.hasAddition) {
      //处理选中的属性的内容
      let arr = Object.keys(this.data.selected);
      console.log('这是选中的sku', arr);
      // //没有选择任何属性，
      if (arr.length != this.data.lunbo.properties.length) {
        wx.showToast({
          title: '请选择属性',
          icon: 'error'
        })
        return false;
      }
      //登录鉴权的功能，用户没有登录的话，提示用户，直接跳转到登录页面去
      if (wx.getStorageSync('token') == '') {
        wx.showToast({
          title: '请先登录',
          icon: 'error'
        })
        wx.navigateTo({
          url: '/pages/me/me',
        })
      }
      //加入购物车效果
      let sku = this.data.selected.map((pid, id) => {
        return {
          optionId: id,
          optionValueId: pid
        }
      })
      console.log('skuu', sku);
      let arrs = []
      sku.forEach(item => {
        arrs.push(item)
      })
      const ggg = JSON.stringify(arrs)
      console.log('sss', ggg);
      let obj = {
        goodsId: this.data.lunbo.basicInfo.id,
        number: this.data.num, //商品数量
        token: wx.getStorageSync('token'),
        sku: ggg
      }
      const res = await API.addgoodslist(obj)
      if (res.code == 0) {
        wx.showToast({
          title: '添加购物车成功哦',
          icon: 'success'
        })
        this.setData({
          show: false
        })
      }
      console.log('加入购物车', res);
    } else {
      // 没有sku
      let obj = {
        goodsId: this.data.lunbo.basicInfo.id,
        number: this.data.num, //商品数量
        token: wx.getStorageSync('token'),
      }
      console.log('ll', obj);
      const res = await API.addgoodslist(obj)
      if (res.code == 0) {
        wx.showToast({
          title: '添加购物车成功哦',
          icon: 'success'
        })
        console.log('加入购物车', res);
      }
    }
    this.setData({
      show: false
    })
  },
  // 计算数值发生改变
  onChange(e) {
    console.log('num', e);
    this.setData({
      num: e.detail
    })
  },

  data: {
    active: 0,
    lunbo: [],
    se: -1, //控制颜色的变量
    show: false, //控制弹框显示
    num: 1, //控制数量
    properties: [], //商品sku属性列表
    selected: [], //选中商品的属性
    content: '',
    arrlist: {}, //价格的变化
    goodsPrice: 0,
  },

  onLoad(options) {
    console.log('跳转过来的id', options.id);
    this.getlist(options.id)
  },

})